package com.riko.comic.module.comic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.riko.comic.core.result.Result;
import com.riko.comic.core.utils.MinioUtil;
import com.riko.comic.module.comic.bean.po.Chapter;
import com.riko.comic.module.comic.bean.po.ChapterImg;
import com.riko.comic.module.comic.bean.po.Comic;
import com.riko.comic.module.comic.service.IChapterImgService;
import com.riko.comic.module.comic.service.IChapterService;
import com.riko.comic.module.comic.service.IComicService;
import com.riko.comic.module.sys.bean.po.User;
import com.riko.comic.module.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author xt
 * @since 2021/10/29
 */
@RestController
@RequestMapping("/file")
public class UploadController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IComicService comicService;

    @Autowired
    private IChapterService chapterService;

    @Autowired
    private IChapterImgService chapterImgService;


    @PostMapping("/upload/path")
    public Result uploadByPath(@RequestBody String path) {

        String url = MinioUtil.uploadFile(path);
        return Result.success(url);

    }

    @PostMapping("/upload")
    public Result uploadByPath(MultipartFile file) {
        try {
            String url = MinioUtil.uploadFile(file);
            return Result.success(url);
        } catch (Exception e) {
            return Result.error();
        }

    }

    @PostMapping("/uploadComic")
    public void uploadComic(HttpServletRequest request, Comic comic, Integer chapterNumber, MultipartFile coverImg, List<MultipartFile> chapterImgList) {
        //上传漫画详情
        String username = (String) request.getSession().getAttribute("loginUser");
        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername,username));
        comic.setUserId(user.getId());
        try {
            String url = MinioUtil.uploadFile(coverImg);
            System.out.println(url);
            comic.setImage(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        comicService.save(comic);

        //上传漫画章节
        Chapter chapter = new Chapter();
        chapter.setComicId(comic.getId());
        chapter.setChapterNumber(chapterNumber);
        chapterService.save(chapter);

        //上传漫画章节图片
        for(MultipartFile image : chapterImgList){
            ChapterImg chapterImg = new ChapterImg();
            chapterImg.setChapterId(chapter.getId());
            chapterImg.setPageNumber(Integer.valueOf(image.getOriginalFilename().split("\\.")[0]));
            String url = null;
            try {
                url = MinioUtil.uploadFile(image);
            } catch (Exception e) {
                e.printStackTrace();
            }
            chapterImg.setImage(url);

            chapterImgService.save(chapterImg);

        }

    }


}
